<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { useStore } from "vuex";
import { getSchoolInfo } from "./api/Infomange";
const { dispatch, commit, state } = useStore();

onLaunch(async () => {
  console.log("App Launch");
  const user = uni.getStorageSync("userInfo");
  if (!user || !user.TOKEN) {
    await dispatch("logout");
  } else {
    try {
      await dispatch("login");
      const storageUserInfo = uni.getStorageSync("userInfo");
      console.log({ storageUserInfo });
      if (storageUserInfo) {
        commit("SET_USER_INFO", storageUserInfo);
      }
    } catch (err) {
      await dispatch("logout");
    }
  }
  uni.addInterceptor("navigateTo", {
    //添加全局跳转拦截 监听用户是否在其他地方登陆
    success(res) {
      getSchoolInfo({
        OPERATOR_ID: state.user.userInfo.CUSER_ID || "0",
      }).catch((err) => {
        //token出错返回主页面并刷新
        if (err.data.error == "invalid_token") {
          setTimeout(() => {
            uni.showToast({
              title: "你已在其他设备登录",
              icon: "none",
              duration: 5000,
              mask: true,
            });
            dispatch("logout");
            setTimeout(() => {
              uni.switchTab({ url: "/pages/index/index" });
              getSchoolInfo({
                //重新获取首页信息，游客
                OPERATOR_ID: "0",
              });
            }, 5000);
          }, 1000);
        }
      });
    },
    fail(err) {},
  });
});
onShow(() => {
  console.log("App Show");
});
onHide(() => {
  console.log("App Hide");
});
</script>
<style>
@import "colorui/main.css";
@import "colorui/icon.css";
.flex-shrink {
  flex-shrink: 0 !important;
}
.float-btn {
  position: fixed;
  bottom: 20rpx;
}
.sticky-top {
  position: sticky;
  top: 0;
}
#app {
  font-family: PingFang SC, serif;
}
page {
  background: rgba(247, 248, 252, 1);
}
</style>
